package com.example.sixtrainingdemo.view;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.SaleGoodsListAdapter;
import com.example.sixtrainingdemo.base.BaseFragment;
import com.example.sixtrainingdemo.contract.MainContract;
import com.example.sixtrainingdemo.entity.Goods;
import com.example.sixtrainingdemo.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

public class SecondFragment extends BaseFragment implements MainContract.IMainView, View.OnClickListener {

   private RecyclerView mGoodsRecyclerView;
   private EditText mSearchEdit;
   private SaleGoodsListAdapter mRecyclerAdapter;
   private MainContract.IMainPresenter mMainPresenter;
   private List<Goods> mGoodsList;

   private Button mButton1;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_home1;
   }

   @Override
   protected void initView() {
      mMainPresenter = new MainPresenter(this, getContext());
      mGoodsList = new ArrayList<>();
      mSearchEdit = mView.findViewById(R.id.search_edit);
      mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
      mButton1 = mView.findViewById(R.id.gosale1);
      StaggeredGridLayoutManager staggeredGridLayoutManager =
              new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
      mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
      mRecyclerAdapter = new SaleGoodsListAdapter(mGoodsList);
      mGoodsRecyclerView.setAdapter(mRecyclerAdapter);
      mMainPresenter.getGoodsList();
   }

   @Override
   protected void initListener() {
      mButton1.setOnClickListener(this);
   }

   @Override
   public void onResume() {
      super.onResume();
      mMainPresenter.getGoodsList();
   }

   @Override
   public void showGoodsList(List<Goods> goodsList) {
      mGoodsList = goodsList;
      Log.d("", "showGoodsList " + mGoodsList.size());
      mRecyclerAdapter.updateData(mGoodsList);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.gosale1) {
         Intent intent = new Intent(mContext, MainActivity.class);
         startActivity(intent);
      }
   }
}
